package com.example.smartcity.ui.frg

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import com.example.smartcity.R
import com.example.smartcity.adapter.ServiceGridAdapter
import com.example.smartcity.app.App
import com.example.smartcity.http.ServiceResp
import com.example.smartcity.ui.act.TOFHomeActivity
import kotlinx.android.synthetic.main.fragment_main_tab2.*

/**
 * @Author: Tailyou(祝文飞)
 * @CreateDate: 2021/10/7 14:53
 * @Description: 全部服务
 */
class MainEnterFrg2 : Fragment(R.layout.fragment_main_tab2) {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        loadServiceData()
    }

    // 读取全部服务数据
    private fun loadServiceData() {
        App.fetchData<ServiceResp>(
            "/prod-api/api/service/list",
            ServiceResp::class.java
        ) {
            renderServiceData(it)
        }
    }

    // 渲染全部服务数据
    private fun renderServiceData(serviceResp: ServiceResp) {
        rvServiceGrid.layoutManager = GridLayoutManager(activity, 4)
        rvServiceGrid.adapter =
            ServiceGridAdapter(serviceResp.rows, object : ServiceGridAdapter.OnItemClickListener {
                override fun onClick(position: Int) {
                    if (position == 6) {
                        val intent = Intent(activity, TOFHomeActivity::class.java)
                        startActivity(intent)
                    }
                }
            }, R.layout.layout_item_grid_service_all)
    }

}